package com.zbbt.sportslottery.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.bean.DialogBean;
import com.zbbt.sportslottery.bean.DialogBean.OnDialogClickListener;
import com.zbbt.sportslottery.ui.widget.DatePickerLayout;
import com.zbbt.sportslottery.ui.widget.SimpleCustomDialog;

public class DialogUtil {
	private static View contentView;

	/**
	 * 
	 * @MethodName:showNobtnDialog
	 * @Description: 通用的没有按钮的弹窗（可自己去控制按钮显示）用于版本更新 下载
	 * @author：LiZhimin
	 * @date：2014-5-20 下午6:04:35
	 * @param context
	 * @param title
	 * @param contentView
	 *            void
	 */
	public static SimpleCustomDialog showNobtnDialog(Context context, CharSequence title, View contentView) {
		return showConfirmExpandDialog(context, title, contentView, "", "", null, null);
	}

	/**
	 * 
	 * @MethodName:showSingleBtnDialog
	 * @Description: 通用的一个按钮的弹窗（只显示右边红色按钮）
	 * @author：LiZhimin
	 * @date：2014-5-17 下午6:36:51
	 * @param context
	 * @param title
	 * @param content
	 * @param rbtText
	 * @param rbtnListener
	 *            void
	 */
	public static void showSingleBtnDialog(Context context, CharSequence title, CharSequence content,
			CharSequence rbtText, OnDialogClickListener rbtnListener) {
		showConfirmDialog(context, title, content, "", rbtText, null, rbtnListener);
	}

	/**
	 * 
	 * @MethodName:showConfirmDialog
	 * @Description: 通用的按钮弹框，标题，中间内容为文字，左右文字按钮
	 * @author：LiZhimin
	 * @date：2014-5-17 下午5:50:07
	 * @param context
	 * @param title
	 * @param content
	 * @param lbtText
	 * @param rbtText
	 * @param lbtnListener
	 * @param rbtnListener
	 *            void
	 */
	public static SimpleCustomDialog showConfirmDialog(Context context, CharSequence title,
			CharSequence content, CharSequence lbtText, CharSequence rbtText,
			OnDialogClickListener lbtnListener, OnDialogClickListener rbtnListener) {
		contentView = View.inflate(context, R.layout.dialog_expand_text, null);
		TextView tv_content = (TextView) contentView.findViewById(R.id.tv_content);
		tv_content.setText(content);
		return showConfirmExpandDialog(context, title, contentView, lbtText, rbtText, lbtnListener,
				rbtnListener);
	}

	/**
	 * 
	 * @MethodName:showConfirmExpandDialog
	 * @Description: 可扩展的通用按钮弹框，标题，自定义中间布局内容，左右文字按钮
	 * @author：LiZhimin
	 * @date：2014-5-17 下午5:49:55
	 * @param context
	 * @param title
	 * @param contentView
	 * @param lbtText
	 * @param rbtText
	 * @param lbtnListener
	 * @param rbtnListener
	 *            void
	 */
	public static SimpleCustomDialog showConfirmExpandDialog(Context context, CharSequence title,
			View contentView, CharSequence lbtText, CharSequence rbtText, OnDialogClickListener lbtnListener,
			OnDialogClickListener rbtnListener) {
		DialogBean dialogBean = new DialogBean(title, lbtText, rbtText, contentView, lbtnListener,
				rbtnListener, DialogBean.STYLE_BTN);
		return baseConfirmDialog(context, dialogBean);
	}

	/**
	 * 
	 * @MethodName:baseConfirmDialog
	 * @Description: 通用的弹框基础方法
	 * @author：LiZhimin
	 * @date：2014-5-15 下午4:57:08
	 * @param context
	 * @param dialogBean
	 *            void
	 */
	public static SimpleCustomDialog baseConfirmDialog(Context context, DialogBean dialogBean) {
		SimpleCustomDialog.Builder.dialogBean = dialogBean;
		SimpleCustomDialog dialog = new SimpleCustomDialog.Builder(context).create();
		dialog.show();
		return dialog;
	}

	public interface OnDatePickedListener {
		void pick(String pickedDate);
	}

	/**
	 * 
	 * @MethodName:showDatePickerDialog
	 * @Description: 弹出选择日期对话框
	 * @author：LiZhimin
	 * @date：2014-8-7 下午10:08:35
	 * @param context
	 * @param dateEditText
	 * @param defaultDate
	 * @param listener
	 *            void
	 */
	public static void showDatePickerDialog(Context context, final TextView dateTextView,
			Calendar defaultDate, final OnDatePickedListener listener) {
		View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_expand_datepicker, null);
		final DatePickerLayout datePicker = (DatePickerLayout) contentView
				.findViewById(R.id.date_picker_layout);
		if (defaultDate != null) {
			datePicker.setYear(defaultDate.get(Calendar.YEAR));
			datePicker.setMonth(defaultDate.get(Calendar.MONTH));
			datePicker.setMonthOfDay(defaultDate.get(Calendar.DAY_OF_MONTH));
		}
		showConfirmExpandDialog(context, "选择日期", contentView, "取消", "确定", null, new OnDialogClickListener() {

			@Override
			public boolean onClick(View v) {
				int year = datePicker.getYear();
				int month = datePicker.getMonth();
				int day = datePicker.getMonthOfDay();
				Calendar calendar = Calendar.getInstance();
				calendar.set(year, month, day);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String pickedDate = sdf.format(calendar.getTime());
				if (listener != null) {
					listener.pick(pickedDate);
				} else {
					dateTextView.setText(pickedDate);
				}
				return false;
			}
		});
	}

	/**
	 * 
	 * @MethodName:showUserHDQDialog
	 * @Description: 弹出活动兑换券对话框
	 * @author wwh
	 * @date：2014-12-31
	 * @param context
	 * @param dateEditText
	 * @param defaultDate
	 * @param listener
	 *            void
	 */
	public static void showUserHDQDialog(Context context, String name, String price, String redeem_code,
			String expire_time, String status, final OnshowUserListener showUserlistener) {
		View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_isusehdq, null);
		TextView nametx = (TextView) contentView.findViewById(R.id.name);
		TextView pricetx = (TextView) contentView.findViewById(R.id.price);
		TextView redeem_codetx = (TextView) contentView.findViewById(R.id.redeem_code);
		TextView expire_timetx = (TextView) contentView.findViewById(R.id.expire_time);
		TextView statustx = (TextView) contentView.findViewById(R.id.status);
		if (status.equals("已使用")) {
			statustx.setTextColor(context.getResources().getColor(R.color.red));
		}
		statustx.setText(status);
		nametx.setText( name);
		pricetx.setText(price);
		redeem_codetx.setText(redeem_code);
		expire_timetx.setText(expire_time);
		showConfirmExpandDialog(context, "兑换券", contentView, "取消", "确定", new OnDialogClickListener() {
			
			@Override
			public boolean onClick(View v) {
				showUserlistener.onPositiveButtonClick();
				return false;
			}
		}, new OnDialogClickListener() {
			@Override
			public boolean onClick(View v) {
				showUserlistener.onNegativeButtonClick();
				return false;
			}
		});

	}

	public interface OnshowUserListener {
		public void onPositiveButtonClick();

		public void onNegativeButtonClick();
	}
}
